package GUI; //done

import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Client.ClientAbstractWindow;
import Client.ConnectionToServer;
import Common.Settings;

import javax.swing.JLabel;

/**
 * This Class controls the Reseller functions.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class ResellerMenu extends ClientAbstractWindow{
	/** The serial Version UID. */
	private static final long serialVersionUID = -4117657374226987002L;
	
	/** The Add Product Button. */
	private JButton addProductButton = null;
	
	/** The Update Product Button. */
	private JButton updateProductButton = null;
	
	/** The Remove Product Button. */
	private JButton removeProductButton = null;
	
	/** The Create Template Button. */
	private JButton createTemplateButton = null;
	
	/** The Update Template Button. */
	private JButton updateTemplateButton = null;
	
	/** The Add Product Button. */
	private JButton activityTrackingButton = null;
	
	/** The Add Product Button. */
	private JButton updateCustomerInterestsButton = null;	
	
	/** Label For Background and pictures. */
	private JLabel label_1;
		
	/**
	 * The default constructor
	 */
	public ResellerMenu() {
		super();
		setBackground(SystemColor.activeCaption);
		initialize();	
	}
	
	/**
	 * This function initializes ResellerMenuForm
	 */
	private void initialize() {
		this.setSize(600, 480);
		this.setLayout(null);
		getUsernameLabel().setText("Welcome "+Settings.Logged_Username);
		this.add(getAddProductButton(),null);
		this.add(getUpdateProductButton(),null);
		this.add(getRemoveProductButton(),null);
		this.add(getCreateTemplateButton(),null);
		this.add(getUpdateTemplateButton(),null);
		this.add(getActivityTrackingButton(),null);
		this.add(getBtnUpdateCustomerInterests(),null);
		add(getLabel_1_1());
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(ResellerMenu.class.getResource("/Pictures/bg.png")));
		label.setBounds(0, 0, 600, 480);
		add(label);
	}
	
	/**
	 * This function initializes addProductButton	
	 */
	private JButton getAddProductButton() {
		if (addProductButton == null) {
			addProductButton = new JButton();
			addProductButton.setBounds(new Rectangle(28, 188, 135, 29));
			addProductButton.setText("Add Product");
			addProductButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					try{
						JFrame jframe = new JFrame();
						ProductWindow productform = new ProductWindow();
						jframe.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
						jframe.setBounds(Settings.Screen_x, Settings.Screen_y, 400, 300 ); 
						jframe.setTitle("Add Product");
						productform.addSaveButton();
						productform.getcancelButton().addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {						
							}
						});		
						productform.getcancelButton().setVisible(false);
						
						JLabel backgroundLabel = new JLabel("");
						backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
						backgroundLabel.setBounds(0, 0, 400, 330);
						productform.add(backgroundLabel);
						
						jframe.setContentPane(productform);
						jframe.setVisible( true );
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
					}
				}
			});
		}
		return addProductButton;
	}
	
	/**
	 * This function initializes updateProductButton
	 */
	private JButton getUpdateProductButton() {
		if (updateProductButton == null) {
			updateProductButton = new JButton();
			updateProductButton.setBounds(new Rectangle(173, 188, 135, 29));
			updateProductButton.setText("Update product");
			updateProductButton.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					try{
						ProductSearchWindow productsearch = new ProductSearchWindow("UpdateProduct");
						productsearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						productsearch.setBounds(Settings.Screen_x, Settings.Screen_y, 400, 350 );
						
						JLabel backgroundLabel = new JLabel("");
						backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
						backgroundLabel.setBounds(0, 0, 400, 330);
						productsearch.add(backgroundLabel);
						
						productsearch.setVisible( true );
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
					}	
				}
			});
		}
		return updateProductButton;
	}
	
	/**
	 * This function initializes removeProductButton
	 */
	private JButton getRemoveProductButton() {
		if (removeProductButton == null) {
			removeProductButton = new JButton();
			removeProductButton.setBounds(new Rectangle(318, 188, 135, 29));
			removeProductButton.setText("Remove product");
			removeProductButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					try{
						ProductSearchWindow productsearch = new ProductSearchWindow("removeProduct");
						productsearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						productsearch.setBounds(Settings.Screen_x, Settings.Screen_y, 400, 350 );
						
						JLabel backgroundLabel = new JLabel("");
						backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
						backgroundLabel.setBounds(0, 0, 400, 330);
						productsearch.add(backgroundLabel);
						
						productsearch.setVisible( true ); 
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR");
					}	
				}
			});
		}
		return removeProductButton;
	}
	
	/**
	 * This function initializes CreateTemplateButton
	 */
	private JButton getCreateTemplateButton(){
		if (createTemplateButton == null){
			createTemplateButton = new JButton();
			createTemplateButton.setBounds(new Rectangle(28, 243, 135, 29));
			createTemplateButton.setText("Create Template");
			createTemplateButton.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e){			
					try{
						ProductSearchWindow productsearch = new ProductSearchWindow("CreateTemplate");
						productsearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						productsearch.setBounds(Settings.Screen_x, Settings.Screen_y, 520, 300 ); // set frame size
						
						JLabel backgroundLabel = new JLabel("");
						backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
						backgroundLabel.setBounds(0, 0, 520, 330);
						productsearch.add(backgroundLabel);
						
						productsearch.setVisible( true ); // display frame
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR","",JOptionPane.ERROR_MESSAGE);
					}	
				}
			});
		}
		return createTemplateButton;
	}
	
	/**
	 * This function initializes UpdateTemplateButton
	 */
	private JButton getUpdateTemplateButton() {
		if (updateTemplateButton == null) {
			updateTemplateButton = new JButton();
			updateTemplateButton.setBounds(new Rectangle(173, 243, 135, 29));
			updateTemplateButton.setText("Update Template");
			updateTemplateButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					try{						
						TemplateSearchWindow templatesearch = new TemplateSearchWindow("UpdateTemplate");
						templatesearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						templatesearch.setBounds(Settings.Screen_x, Settings.Screen_y, 520, 300 ); // set frame size
						
						JLabel backgroundLabel = new JLabel("");
						backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
						backgroundLabel.setBounds(0, 0, 520, 330);
						templatesearch.add(backgroundLabel);
						
						templatesearch.setVisible( true ); // display frame
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					}	
				}
			});
		}
		return updateTemplateButton;
	}
	
	/**
	 * This function initializes activityTrackingButton
	 */
	private JButton getActivityTrackingButton() {
		if (activityTrackingButton  == null) {
			activityTrackingButton  = new JButton();
			activityTrackingButton .setBounds(new Rectangle(28, 353, 188, 29));
			activityTrackingButton .setText("Activity tracking");
			activityTrackingButton .addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					try {
						ConnectionToServer connectionToServer = new ConnectionToServer();
						String str = (String)connectionToServer.Activitytracking();
						if (str != null){
							if (str.equals("OK"))
								JOptionPane.showMessageDialog(null, "Activity Tracking done");
							else
								JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);
						}
					} 
					catch (Exception e1) {
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					}
				}
			});
		}
		return activityTrackingButton ;
	}
	
	/**
	 * This function initializes BtnUpdateCustomerInterests
	 */
	private JButton getBtnUpdateCustomerInterests() {
		if (updateCustomerInterestsButton  == null) {
			updateCustomerInterestsButton  = new JButton();
			updateCustomerInterestsButton .setBounds(new Rectangle(28, 299, 188, 29));
			updateCustomerInterestsButton .setText("Update Customer Interests");
			updateCustomerInterestsButton .addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					try{
						CustomerSearchWindow customersearch = new CustomerSearchWindow("AddCustomerInterests");
						customersearch.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
						customersearch.setBounds(Settings.Screen_x, Settings.Screen_y, 400, 280); // set frame size
						customersearch.setVisible( true ); // display frame
					}
					catch(HeadlessException headlessexception){
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					}	
				}
			});
		}
		return updateCustomerInterestsButton;
	}
	
	/**
	 * This function initializes Label_1_1
	 */
	private JLabel getLabel_1_1(){
		if (label_1 == null) {
			label_1 = new JLabel("");
			label_1.setIcon(new ImageIcon(ResellerMenu.class.getResource("/Pictures/reseller-icon.png")));
			label_1.setBounds(41, 33, 324, 107);
		}
		return label_1;
	}
}
